package feevale.dropbox.view;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import java.net.Socket;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;

public class ServerView extends JFrame {
	
	private static final String TITLE_FRM = "Dropbox - Sistemas Distribuídos";
	
	private static final Integer DEFAULT_PORT = 8899;
	private static final String DEFAULT_DIRECTORY = "C:\\Temp\\client";
	
	private JTextField txtHost;
	private JTextField txtPort;
	private JTextField txtDirectory;
	private JButton btnStart;
	private JButton btnExit;

	public ServerView() {
		setTitle(TITLE_FRM);
		setSize(400, 200);
		setLayout(null);
		
		initLayout();
		setDefaultValues();
		
		setResizable(false);
		setVisible(true);
		setDefaultCloseOperation(DISPOSE_ON_CLOSE);
	}
	
	/**
	 * 
	 */
	private void initLayout() {
		JLabel lblPort = new JLabel("Porta:");
		lblPort.setBounds(260, 30, 50, 20);
		add(lblPort);
		
		txtPort = new JTextField();
		txtPort.setBounds(320, 30, 50, 20);
		txtPort.setEditable(false);
		add(txtPort);
		
		JLabel lblDirectory = new JLabel("Diretório:");
		lblDirectory.setBounds(20, 70, 80, 20);
		add(lblDirectory);
		
		txtDirectory = new JTextField();
		txtDirectory.setBounds(100, 70, 270, 20);
		add(txtDirectory);
		
		btnStart = new JButton("Iniciar");
		btnStart.setBounds(20, 110, 100, 25);
		add(btnStart);
		
		btnStart.addActionListener(getStartAction());
		
	}
	
	/**
	 * Atribui os valores default para os campos da tela
	 */
	private void setDefaultValues() {
		txtPort.setText(DEFAULT_PORT.toString());
		txtDirectory.setText(DEFAULT_DIRECTORY);
	}
	
	private ActionListener getStartAction() {
		return new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				
				btnStart.setEnabled(false);
//				btnExit.setEnabled(true);
				
				try {
					Socket socket = new Socket(txtHost.getText(), Integer.parseInt(txtPort.getText()));
					
				} catch (NumberFormatException | IOException e1) {
					e1.printStackTrace();
				}
			}
		};
	}
	
}
